<template>
  <div class="home-container">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="logo">旅游信息管理系统</div>
      <div class="user-info">
        <el-dropdown>
          <span class="user-dropdown">
            <el-avatar :size="32" :src="adminInfo.avatar || ''" :icon="adminInfo.avatar ? '' : 'UserFilled'" />
            <span class="username">{{ adminInfo.nickName || '管理员' }}</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </div>

    <div class="main">
      <!-- 左侧菜单 -->
      <div class="sidebar">
        <el-menu
          :default-active="activeMenu"
          class="menu"
          background-color="#304156"
          text-color="#fff"
          router
          @select="handleSelect"
        >
          <el-menu-item index="/data">
            <el-icon><HomeFilled /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="/scenic">
            <el-icon><Location /></el-icon>
            <span>景点管理</span>
          </el-menu-item>
          <el-menu-item index="/order">
            <el-icon><Tickets /></el-icon>
            <span>订单管理</span>
          </el-menu-item>
          <el-menu-item index="/user">
            <el-icon><User /></el-icon>
            <span>用户管理</span>
          </el-menu-item>
          <el-menu-item index="/blog">
            <el-icon><ChatRound /></el-icon>
            <span>动态管理</span>
          </el-menu-item>
          <el-menu-item index="/ticket">
            <el-icon><Ticket /></el-icon>
            <span>门票核销</span>
          </el-menu-item>
        </el-menu>
      </div>

      <!-- 主内容区 -->
      <div class="content">
        <router-view></router-view>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted, onBeforeMount } from 'vue'
import { ElMessage } from 'element-plus'
import { HomeFilled, Location, Tickets, User, ChatRound, Ticket } from '@element-plus/icons-vue'
import { useRouter } from 'vue-router'

const router = useRouter()

const activeMenu = ref('/data')

// 从sessionStorage获取管理员信息
const adminInfo = ref({})

onBeforeMount(() => {
  // 检查是否有管理员登录信息
  const adminData = sessionStorage.getItem('admin')
  if(!adminData){
    ElMessage.error('请先登录')
    router.push('/login')
    return
  }
  
  try {
    // 解析登录信息
    adminInfo.value = JSON.parse(adminData)
  } catch (error) {
    ElMessage.error('登录信息无效，请重新登录')
    sessionStorage.removeItem('admin')
    router.push('/login')
  }
})

onMounted(() => {
  // 根据当前路由设置激活的菜单项
  activeMenu.value = router.currentRoute.value.path
})

const userInfo = reactive({
  username: '管理员'
})

const handleLogout = () => {
  ElMessage.success('退出成功')
  sessionStorage.removeItem('admin')
  router.push('/login')
}

const handleSelect = (index) => {
  activeMenu.value = index
}
</script>

<style scoped>
.home-container {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  height: 60px;
  background-color: #fff;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 20px;
  box-shadow: 0 1px 4px rgba(0,21,41,.08);
}

.logo {
  font-size: 20px;
  font-weight: bold;
  color: #304156;
}

.user-info {
  cursor: pointer;
}

.user-dropdown {
  display: flex;
  align-items: center;
  gap: 8px;
}

.username {
  font-size: 14px;
  color: #606266;
}

.main {
  flex: 1;
  display: flex;
}

.sidebar {
  width: 200px;
  background-color: #304156;
  height: 100%;
}

.menu {
  border-right: none;
}

.content {
  flex: 1;
  padding: 20px;
  background-color: #f0f2f5;
}
</style>

